package com.efuture.androidmvpdemo.main

import android.os.Bundle
import android.view.View
import com.efuture.androidmvpdemo.databinding.ActivityMainBinding
import com.efuture.androidmvpdemo.ui.base.BaseActivity
import com.efuture.androidmvpdemo.ui.adapter.HomeDataAdapter
import com.efuture.androidmvpdemo.ui.main.MainInteractor
import com.efuture.androidmvpdemo.ui.main.MainPresenter
import com.efuture.androidmvpdemo.ui.main.MainView
import com.efuture.netrequestdemo.bean.Project
import com.efuture.netrequestdemo.utils.AppUtils
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel

class MainActivity : BaseActivity(),MainView {
    private val mainScope = MainScope()
    private var binding: ActivityMainBinding? = null
    private val presenter = MainPresenter(this,MainInteractor())
    private var homeDataAdapter:HomeDataAdapter? = null;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //关键代码
        binding = ActivityMainBinding.inflate(layoutInflater)
        val rootView: View = binding!!.getRoot()
        setContentView(rootView)

        loadData()

    }

    //加载数据
    private fun loadData() {
        homeDataAdapter = HomeDataAdapter(null)
        binding?.rvHomeData?.adapter  = homeDataAdapter
        presenter.getHomepageData(mainScope)


    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.onDestroyView(mainScope)


    }

    override fun loadDataSuccess(projectData: Project) {
        ToastUtils.showShort("获取主页数据成功")
        homeDataAdapter?.updateList(projectData.data.datas)

    }

    override fun loadDataFail(reason:String) {

        ToastUtils.showShort(reason)

    }
}
